(function() {
    'use strict';

    /** 预约项目类型管理
     * @ngdoc function
     * @name webApp.controller:GzhscCtrl
     * @description 
     * # GzhscCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('GzhscCtrl', GzhscCtrl);

    GzhscCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function GzhscCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);

        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            $scope.pageSize = $scope.pageSize ? $scope.pageSize : 10;
            var query = {
                content: {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize,
                }
            };
            dataService.gzhscGet(query).then(function(re1) {
                if (re1.gzhsc_list) {
                    $scope.gzhsc_list = re1.gzhsc_list;
                    _.map($scope.gzhsc_list, function(da, kk) {
                        if (da.gzhsc_tag_ids) {
                            da.gzhsc_tag_ids = da.gzhsc_tag_ids ? da.gzhsc_tag_ids.split(",") : null;
                        }
                    });
                    $scope.total_count = re1.total_count;
                }
            });
        }

        $scope.getone = function() {
            var query = {
                content: { gzhscs_id: $scope.gzhscs_id }
            };
            dataService.gzhscGet(query).then(function(re1) {
                if (re1.gzhsc_list) {
                    $scope.gzhsc = re1.gzhsc_list[0];
                    $scope.gzhsc.gzhsc_tag_ids = $scope.gzhsc.gzhsc_tag_ids ? $scope.gzhsc.gzhsc_tag_ids.split(",") : null;
                }
            });
        }

        //添加数据 初始化
        $scope.addAction = function() {
            $scope.uid = random();
            $scope.gzhscs_id = $location.search().gzhscs_id;
            if ($scope.gzhscs_id) {
                $scope.getone();
            } else {
                $scope.gzhsc = { name: null, gzhscs_id: 0, content: "" };
            }
        }

        $scope.operation = function(type, title, id) {
            helper.myConfirm("确定" + title, function() {
                var data = { content: { "gzhscs_id": id } };
                $scope.postapi(type, data);
            });
        }

        $scope.saveData = function() {

            var gzhsc_tag_ids;
            try {
                gzhsc_tag_ids = $scope.gzhsc.gzhsc_tag_ids.toString();
            } catch (e) {
                gzhsc_tag_ids = $scope.gzhsc.gzhsc_tag_ids ? $scope.gzhsc.gzhsc_tag_ids[0] : "";
            }

            var api = "add";
            var data = {
                content: {
                    "gzhscs_id": $scope.gzhsc.gzhscs_id,
                    "gzhsc_cat_id": $scope.gzhsc.gzhsc_cat_id,
                    "gzhsc_tag_ids": gzhsc_tag_ids,
                    "content": $scope.gzhsc.content,
                    "status": $scope.gzhsc.status,
                    "title": $scope.gzhsc.title,
                }
            }

            $scope.postapi(api, data);
        }

        $scope.postapi = function(type, data) {
            var zhuoziApiAry = {
                "add": "gzhscAdd",
                "del": "gzhscDelete",
            }

            dataService[zhuoziApiAry[type]](data).then(function(r) {
                if (r.Msg) {
                    helper.myAlert(r.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $scope.getlist(1);
                        $state.go("gzhsc.list");
                    });
                }

            })
        }
    }
})();